package com.opensourceedge.phonebook.cli;

import java.io.BufferedReader;
import java.io.InputStreamReader;

import com.opensourceedge.phonebook.api.Contact;
import com.opensourceedge.phonebook.api.FileStore;
import com.opensourceedge.phonebook.api.Store;

public class DeleteContactCommand implements Command {

	Contact contactToDelete;
	final String COMMAND="del";
	public static String helpText;
	
	public DeleteContactCommand() {
		contactToDelete = new Contact();
		helpText="'"+COMMAND+"' to Delete Contact.";
	}

	@Override
	public void execute() {
		//System.out.println("inside modifyContactcommand");
		Store cs = new FileStore("/contacts.txt");
		// gets input from command.
		System.out.print("Enter Contact ID :");
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		try {
			String input = br.readLine();
			contactToDelete = cs.select(Integer.parseInt(input));
			if(contactToDelete==null){
				throw new Exception("Contact doesn't exist Exception");
			}
			cs.delete(contactToDelete.getContactID());
			System.out.println("<<Success>> Done");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			System.out.println("<ERROR> Contact ID doesnt exists.");
		}
	}
	
	


	@Override
	public String getCommandCode() {
		return COMMAND;
	}

	@Override
	public String getCommandHelpText() {
		return helpText;
	}
}
